package com.halflife.comeElectricity.batteryCabinet;

import com.google.gson.Gson;
import com.halflife.comeElectricity.batteryBox.LockerResult;
import com.halflife.comeElectricity.batteryBox.Result;
import com.halflife.framework.converter.MapHandler;
import com.halflife.framework.domain.Page;
import com.halflife.framework.participant.Department;
import com.halflife.framework.participant.DepartmentRepository;
import com.halflife.framework.participant.DepartmentService;
import com.halflife.framework.participant.user.UserView;
import com.halflife.shiro.utils.ShiroUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class BatteryCabinetAdminController {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private BatteryCabinetRepository batteryCabinetRepository;
    @Resource
    private DepartmentRepository departmentRepository;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private RestTemplate template;
    @RequestMapping("/batteryCabinet/status")
    public String gotoStatus(Integer id,String no,Integer batteryCabinetId,HttpServletResponse response,
                             HttpServletRequest request, Model model) {
        Gson gson = new Gson();
        String result = template.getForObject("http://47.105.65.229:9898/queryAllState", String.class);
        LockerResult s =  gson.fromJson(result, LockerResult.class);
        model.addAttribute("list", s.allState);
        model.addAttribute("id", id);
        model.addAttribute("no", no);
        return "batteryCabinet/status";
    }
    @RequestMapping("/batteryCabinet/list")
    public String gotoList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {
        UserView user = ShiroUtil.getUserView(request);
        MapHandler.handleMap(queryParam);
        queryParam.put("departmentId", user.getDepartmentId());
        if (page == null) {
            page = new Page();
        }
        page.setQueryParam(queryParam);
        int totalCount = batteryCabinetRepository.selectCount(queryParam);
        List<BatteryCabinet> list = batteryCabinetRepository.selectListByPage(queryParam);
        RestTemplate template = new RestTemplate();


        page.setTotalCount(totalCount);
        page.setMode(model);
        model.addAttribute("list",list);
        model.addAttribute("queryParam",queryParam);
        return "batteryCabinet/list";
    }

    @RequestMapping("/batteryCabinet/operate")
    public String gotoOperate(Integer id,HttpServletResponse response,
                               HttpServletRequest request, Model model) {
        departmentService.initDepartment(request,model);
        BatteryCabinet batteryCabinet = new BatteryCabinet();
        if(id!=null){
            batteryCabinet = batteryCabinetRepository.selectByPrimaryKey(id);
        }

        model.addAttribute("batteryCabinet",batteryCabinet);
        return "batteryCabinet/operate";
    }

    @PostMapping("/batteryCabinet/save")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity save(BatteryCabinet batteryCabinet, HttpServletRequest request,
                              HttpServletResponse response, Model model) {
        try {
            if(batteryCabinet.getId()!=null){
                batteryCabinetRepository.updateByPrimaryKey(batteryCabinet);
            }else {
                batteryCabinet.init();
                Integer a = batteryCabinetRepository.insert(batteryCabinet);
            }
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }

    @DeleteMapping("/batteryCabinet/delete")
    public ResponseEntity setRiderState(Integer id, Integer state) {
        try {
            batteryCabinetRepository.deleteByPrimaryKey(id);
//            BatteryCabinet batteryCabinet = new BatteryCabinet();
//            batteryCabinet.setId(id);
//            batteryCabinet.setValid(Boolean.FALSE);
//            batteryCabinetRepository.updateByPrimaryKeySelective(batteryCabinet);
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }
    @GetMapping("/batteryCabinet/setBoxState")
    public ResponseEntity setBoxState1(Long lockerId, Integer boxId,Integer invalid) {
        try {
            Gson gson = new Gson();
            String result = template.getForObject("http://47.105.65.229:9898/setState?lockerId="+lockerId+"&boxId="+boxId+"&alarm=0&invalid="+invalid, String.class);

        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }
}
